using System;
using System.Collections;
using System.IO;

using Game.Input;
using Game.Output;

namespace Game.Utils
{
	public class CTextFile
	{
		private string		m_path;
		private ArrayList	m_lines;
		private string		m_text;
		
		public CTextFile(string p_path)
		{
			CConsole.ReportClassInit(System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.Name);
			CConsole.ReportParam("p_path",  p_path);
			
			m_path = p_path;
			
			m_lines	= new ArrayList();
			m_text	= "";
		}
		
		public void AddLine(string p_line)
		{
			m_lines.Add(p_line);
		}
	    
	    public void LoadLines()
		{
			System.IO.StreamReader file = new System.IO.StreamReader(m_path); 
			
			while (file.Peek() > -1)
			{
				m_lines.Add(file.ReadLine());
			}
			
			file.Close();
		}
	    
	    public void SaveLines()
		{
	    	string[] lines;
	    	lines = (string[])m_lines.ToArray(typeof(string));
	    	
			StreamWriter file = new StreamWriter(m_path);
			
			for (int i = 0; i < lines.Length; i++)
			{
				file.Write(lines[i]);
				
				if (i < (lines.Length - 1))
					file.WriteLine();
			}
	        
	        file.Close();
		}
	 
	    public void LoadString()
	    {
	        if (File.Exists(m_path))
	        {
	            StreamReader file = new StreamReader(m_path);
	            m_text = file.ReadToEnd();
	            file.Close();
	        }
	    }
	    
	    public void SaveString()
	    {
	        StreamWriter file = new StreamWriter(m_path);
	        file.Write(m_text);
	        file.Close();
	    }
	    
	    # region properties
	    
	    public string Path
		{
			get {
				return m_path;
			}
		}
		
		public ArrayList Lines
		{
			get {
				return m_lines;
			}
		}
		
		public string Text
		{
			get {
				return m_text;
			}
		}
		
		# endregion
		
	}
}
